﻿/*-------------------------------------------------------------------------
* 命名空间: WaferSelectV3.ViewModels/ExceptionDialogViewModel
* 类       名: ExceptionDialogViewModel
* 功      能:  TD
* 时      间:  2024-04-25 15:18:08
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Text;
using System.Threading.Tasks;
using WaferSelectV3.Dialogs;
using WaferSelectV3.Services;

namespace WaferSelectV3.ViewModels
{
    public class ExceptionDialogViewModel : BindableBase, IDialogAware
    {
        public ExceptionDialogViewModel()
        {
            ViewDetailCommand = new DelegateCommand(DoViewDetail);
            CloseDialogCommand = new DelegateCommand(DoCloseDialogCommand);
        }


        public DelegateCommand ViewDetailCommand { get; private set; }
        public DelegateCommand CloseDialogCommand { get; private set; }


        private void DoViewDetail()
        {
            DialogParameters parameters = new DialogParameters()
            {
                {"Title","异常详情页" },
                { "Exception",ExceptionDetail}
            };
            DialogProvider.ShowDialog(nameof(ExceptionDetailDialog), parameters);
        }
        private void DoCloseDialogCommand()
        {
            RequestClose?.Invoke(null!);
        }


        private string? title;

        public string? Title
        {
            get { return title; }
            set
            {
                title = value;
                RaisePropertyChanged();
            }
        }

        private string? message;

        public string? Message
        {
            get { return message; }
            set
            {
                message = value;
                RaisePropertyChanged();
            }
        }

        private Exception? exceptionDetail;

        public Exception? ExceptionDetail
        {
            get { return exceptionDetail; }
            set
            {
                exceptionDetail = value;
                RaisePropertyChanged();
            }
        }




        public event Action<IDialogResult>? RequestClose;

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {

        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            if (parameters != null)
            {
                parameters.TryGetValue("Title", out string title);
                parameters.TryGetValue("Exception", out Exception exception);
                if (!string.IsNullOrEmpty(title) && exception != null)
                {
                    Title = title;
                    ExceptionDetail = exception;
                    Message = exception.Message;
                }
            }
        }
    }
}
